package com.kgc.controller;

import com.kgc.common.Result;
import com.kgc.entity.Activities;
import com.kgc.entity.Participants;
import com.kgc.service.interf.ActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/apis")
public class ActivitiesController {
    @Autowired
    private ActivitiesService activitiesService;

    @Autowired
    private FilesOssDemoController filesOssDemoController;//oss

    /**
     * 添加活动
     *
     * @param activities
     * @return
     */
    @RequestMapping("/admin/activity/add")
    public String addAction(@RequestBody Activities activities, MultipartFile imageFile) throws IOException {
        if (imageFile != null) {
            if (imageFile.getSize() > 0) {
                //上传活动图片到oss
                String activityPhoto = filesOssDemoController.upload(imageFile);
                activities.setActivityPhoto(activityPhoto);
            }
        }
        int i = activitiesService.addAction(activities);
        if (i > 0) {
            return Result.success("0", "活动发布成功");
        }
        return Result.success("200", "活动发布失败");
    }

    /**
     * 删除活动
     *
     * @param activityID
     */
    @RequestMapping("/admin/activity/del/{activity_id}")
    public String deleteAction(@PathVariable("activity_id") Integer activityID) {
        int i = activitiesService.deleteAction(activityID);
        if (i > 0) {
            return Result.success("0", "活动删除成功");
        }
        return Result.success("200", "活动删除失败");
    }

    /**
     * 修改活动
     *
     * @param
     * @return
     */

    @RequestMapping("/admin/activity/edit")
    public String updateAction(Activities activities, MultipartFile imageFile) throws IOException {
        if (imageFile != null) {
            if (imageFile.getSize() > 0) {
                String upload = filesOssDemoController.upload(imageFile);
                activities.setActivityPhoto(upload);
            }
        }
        int i = activitiesService.updateAction(activities);
        if (i > 0) {
            return Result.success("0", "活动修改成功");
        }
        return Result.success("400", "不合法参数");
    }


    /**
     * 查询所有活动
     *
     * @return
     */
    @RequestMapping("/user/activity/queryAll")
    public String getAllActivities() {
        List<Activities> allActivities = activitiesService.getAllActivities();
        if (allActivities != null) {
            return Result.success("0", "查询成功", allActivities);
        } else {
            return Result.success("400", "参数不合法");
        }
    }

    /**
     * 用户报名活动
     *
     * @param activityId
     * @param userId
     * @return
     */
    @RequestMapping("/user/activity/join_act")
    public String userSignUp(Integer activityId, Integer userId, String userPhone) {
        int i = activitiesService.userSignUp(activityId, userPhone, userId);
        if (i > 0) {
            return Result.success("0", "报名成功");
        }
        return Result.success("400", "不合法参数");
    }

    /**
     * 查询用户报名的活动
     *
     * @param userId
     * @return
     */
    @RequestMapping("/user/activity/queryByUserId")
    public String getUserSignUpActivities(Integer userId) {
        List<Activities> activities = activitiesService.getUserSignUpActivities(userId);
        if (activities != null) {
            return Result.success("0", "查询成功", activities);
        } else {
            return Result.success("400", "参数不合法");
        }
    }

    /**
     * 查询已结束的活动
     */
    @RequestMapping("/user/activity/queryByEnd")
    public String getEndActivities() {
        List<Activities> activities = activitiesService.getEndActivities();
        if (activities != null) {
            return Result.success("0", "查询成功", activities);
        } else {
            return Result.success("400", "参数不合法");
        }
    }

    /**
     * 查询未结束的活动
     */
    @RequestMapping("/user/activity/queryByStart")
    public String getStartActivities() {
        List<Activities> activities = activitiesService.getStartActivities();
        if (activities != null) {
            return Result.success("0", "查询成功", activities);
        } else {
            return Result.success("400", "参数不合法");
        }
    }

    /**
     * 管理员查询所有活动
     */
    @RequestMapping("/user/activity/queryAll2")
    public String getAllActivities2() {
        List<Activities> allActivities = activitiesService.getAllActivities1();
        if (allActivities != null) {
            return Result.success("0", "查询成功", allActivities);
        } else {
            return Result.success("400", "参数不合法");
        }
    }

}
